package com.easipass.lgsa.portRelease.util;

import com.easipass.bscp.dto.*;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * <dependency>
 * <groupId>com.alibaba</groupId>
 * <artifactId>fastjson</artifactId>
 * <version>1.2.47</version>
 * </dependency>
 */
@Service
public class ParamUtil {

    protected final Log log = LogFactory.getLog(this.getClass());
    private Gson gson = new Gson();
    @Autowired
    RedisTemplate<String, String> redisTemplate;

    private static Map<String, Complex> complexMap;
    private static Map<String, Curr> currMap;
    private static Map<String, ExchangeRateQuery> exchangeratequeryMap;
    private static Map<String, TradeMode> trademodeMap;
    private static Map<String, Transf> transfMap;
    private static Map<String, Transac> transacMap;
    private static Map<String, Custom> customsMap;
    private static Map<String, District> districtMap;
    private static Map<String, Country> countryMap;
    private static Map<String, Unit> unitMap;
    private static Map<String, UseTo> useToMap;
    private static Map<String, Levymode> levymodeMap;
    private static Map<String, Wraptype> wraptypeMap;
    /*public static Map<String,Ports> portstdMap;
    public static Map<String,Levymode> levytypeMap;*/
    private static Map<String, String> versionMap = new HashMap<>();


    public Map<String, Complex> getComplexMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_complex_version") + "";
        if (complexMap != null) {
            if (versionMap.containsKey("param_complex_version")) {
                String str = versionMap.get("param_complex_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> complexKeys = redisTemplate.keys("param_complex_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            complexMap = new HashMap<String, Complex>();
            for (String complexKey : complexKeys) {
                String complexStr = redisTemplate.opsForValue().get(complexKey);
                // Complex complex = JSON.parseObject(complexStr,Complex.class);
                Complex complex = gson.fromJson(complexStr, Complex.class);
                complexMap.put(complexKey, complex);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_complex_version", version);
        }
        return complexMap;
    }

    public Map<String, Curr> getCurrMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_curr_version") + "";
        if (currMap != null) {
            if (versionMap.containsKey("param_curr_version")) {
                String str = versionMap.get("param_curr_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> currKeys = redisTemplate.keys("param_curr_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            currMap = new HashMap<String, Curr>();
            for (String currKey : currKeys) {
                String currStr = redisTemplate.opsForValue().get(currKey);
                // Curr curr = JSON.parseObject(currStr, Curr.class);
                Curr curr = gson.fromJson(currStr, Curr.class);
                currMap.put(currKey, curr);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_curr_version", version);
        }
        //log.info("币制，值："+new Gson().toJson(currMap));
        return currMap;
    }

    public Map<String, ExchangeRateQuery> getExchangeratequeryMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_exchangeratequery_version") + "";
        if (exchangeratequeryMap != null) {
            if (versionMap.containsKey("param_exchangeratequery_version")) {
                String str = versionMap.get("param_exchangeratequery_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> exchangeRateQueryKeys = redisTemplate.keys("param_exchangeratequery_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            exchangeratequeryMap = new HashMap<String, ExchangeRateQuery>();
            for (String exchangeRateQueryKey : exchangeRateQueryKeys) {
                String exchangeRateQueryStr = redisTemplate.opsForValue().get(exchangeRateQueryKey);
                // ExchangeRateQuery exchangeRateQuery = JSON.parseObject(exchangeRateQueryStr, ExchangeRateQuery.class);
                ExchangeRateQuery exchangeRateQuery = gson.fromJson(exchangeRateQueryStr, ExchangeRateQuery.class);
                exchangeratequeryMap.put(exchangeRateQueryKey, exchangeRateQuery);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_exchangeratequery_version", version);
        }
        //log.info("汇率，值："+new Gson().toJson(exchangeratequeryMap));
        return exchangeratequeryMap;
    }

    public Map<String, TradeMode> getTrademodeMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_trademode_version") + "";
        if (trademodeMap != null) {
            if (versionMap.containsKey("param_trademode_version")) {
                String str = versionMap.get("param_trademode_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> trademodeKeys = redisTemplate.keys("param_trademode_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            trademodeMap = new HashMap<String, TradeMode>();
            for (String trademodeKey : trademodeKeys) {
                String trademodeStr = redisTemplate.opsForValue().get(trademodeKey);
                // TradeMode tradeMode = JSON.parseObject(trademodeStr, TradeMode.class);
                TradeMode tradeMode = gson.fromJson(trademodeStr, TradeMode.class);
                trademodeMap.put(trademodeKey, tradeMode);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_trademode_version", version);
        }
        //log.info("贸易方式值："+new Gson().toJson(trademodeMap));
        return trademodeMap;
    }

    public Map<String, Transf> getTransfMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_transf_version") + "";
        if (transfMap != null) {
            if (versionMap.containsKey("param_transf_version")) {
                String str = versionMap.get("param_transf_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> transfKeys = redisTemplate.keys("param_transf_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            transfMap = new HashMap<String, Transf>();
            for (String transfKey : transfKeys) {
                String transfStr = redisTemplate.opsForValue().get(transfKey);
                // Transf transf = JSON.parseObject(transfStr, Transf.class);
                Transf transf = gson.fromJson(transfStr, Transf.class);
                transfMap.put(transfKey, transf);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_transf_version", version);
        }
        //log.info("运输方式，值："+new Gson().toJson(transfMap));
        return transfMap;
    }


    public Map<String, Transac> getTransacMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_transac_version") + "";
        if (transacMap != null) {
            if (versionMap.containsKey("param_transac_version")) {
                String str = versionMap.get("param_transac_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }

        if (sw) {
            Set<String> transacKeys = redisTemplate.keys("param_transac_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            transacMap = new HashMap<String, Transac>();
            for (String transacKey : transacKeys) {
                String transacStr = redisTemplate.opsForValue().get(transacKey);
                // Transac transac = JSON.parseObject(transacStr, Transac.class);
                Transac transac = gson.fromJson(transacStr, Transac.class);
                transacMap.put(transacKey, transac);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_transac_version", version);
        }
        //log.info("成交方式，值："+new Gson().toJson(transacMap));
        return transacMap;
    }


    public Map<String, Custom> getcustomsMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_customs_version") + "";
        if (customsMap != null) {
            if (versionMap.containsKey("param_customs_version")) {
                String str = versionMap.get("param_customs_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> customsKeys = redisTemplate.keys("param_customs_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            customsMap = new HashMap<String, Custom>();
            for (String customsKey : customsKeys) {
                String customsStr = redisTemplate.opsForValue().get(customsKey);
                // Custom custom = JSON.parseObject(customsStr, Custom.class);
                Custom custom = gson.fromJson(customsStr, Custom.class);
                customsMap.put(customsKey, custom);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_customs_version", version);
        }
        //log.info("关区，值："+new Gson().toJson(customsMap));
        return customsMap;
    }


    public Map<String, District> getDistrictMap() {

        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_district_version") + "";
        if (districtMap != null) {
            if (versionMap.containsKey("param_district_version")) {
                String str = versionMap.get("param_district_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> districtKeys = redisTemplate.keys("param_district_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            districtMap = new HashMap<String, District>();
            for (String districtKey : districtKeys) {
                String districtStr = redisTemplate.opsForValue().get(districtKey);
                // District district = JSON.parseObject(districtStr, District.class);
                District district = gson.fromJson(districtStr, District.class);
                districtMap.put(districtKey, district);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_district_version", version);
        }
        //log.info("境内目的地（地区代码），值："+new Gson().toJson(districtMap));
        return districtMap;
    }


    public Map<String, Country> getCountryMap() {

        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_country_version");
        log.info("取到最新缓存：" + version);
        if (countryMap != null) {
            if (versionMap.containsKey("param_country_version")) {
                String str = versionMap.get("param_country_version");
                log.info("原始版本：" + str);
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> countryKeys = redisTemplate.keys("param_country_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            countryMap = new HashMap<String, Country>();
            for (String countryKey : countryKeys) {
                String countryStr = redisTemplate.opsForValue().get(countryKey);
                // log.info("key:" + countryKey + "_val:" + countryStr);
                // Country country = JSON.parseObject(countryStr, Country.class);
                Country country = gson.fromJson(countryStr, Country.class);
                countryMap.put(countryKey, country);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_country_version", version);
        }
        //log.info("国家地区代码，值："+new Gson().toJson(countryMap));
        return countryMap;
    }


    public Map<String, Unit> getUnitMap() {
        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_unit_version") + "";
        if (unitMap != null) {
            if (versionMap.containsKey("param_unit_version")) {
                String str = versionMap.get("param_unit_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> unitKeys = redisTemplate.keys("param_unit_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            unitMap = new HashMap<String, Unit>();
            for (String unitKey : unitKeys) {
                String unitStr = redisTemplate.opsForValue().get(unitKey);
                // Unit unit = JSON.parseObject(unitStr, Unit.class);
                Unit unit = gson.fromJson(unitStr, Unit.class);
                unitMap.put(unitKey, unit);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_unit_version", version);
        }
        //log.info("计量单位，值："+new Gson().toJson(unitMap));
        return unitMap;
    }


    public Map<String, UseTo> getUsetoMap() {

        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_use_to_version") + "";
        if (useToMap != null) {
            if (versionMap.containsKey("param_use_to_version")) {
                String str = versionMap.get("param_use_to_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> useToKeys = redisTemplate.keys("param_use_to_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            useToMap = new HashMap<String, UseTo>();
            for (String useTokey : useToKeys) {
                String useToStr = redisTemplate.opsForValue().get(useTokey);
                // UseTo useTo = JSON.parseObject(useToStr, UseTo.class);
                UseTo useTo = gson.fromJson(useToStr, UseTo.class);
                useToMap.put(useTokey, useTo);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_use_to_version", version);
        }
        //log.info("用途代码，值："+new Gson().toJson(useToMap));
        return useToMap;
    }


    public Map<String, Levymode> getLevymodeMap() {

        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_levymode_version") + "";
        if (levymodeMap != null) {
            if (versionMap.containsKey("param_levymode_version")) {
                String str = versionMap.get("param_levymode_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> levymodeKeys = redisTemplate.keys("param_levymode_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            levymodeMap = new HashMap<String, Levymode>();
            for (String levymodeKey : levymodeKeys) {
                String levymodeStr = redisTemplate.opsForValue().get(levymodeKey);
                // Levymode levymode = JSON.parseObject(levymodeStr, Levymode.class);
                Levymode levymode = gson.fromJson(levymodeStr, Levymode.class);
                levymodeMap.put(levymodeKey, levymode);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_levymode_version", version);
        }
        //log.info("征减免方式，值："+new Gson().toJson(levymodeMap));
        return levymodeMap;
    }


    public Map<String, Wraptype> getWraptypeMap() {

        boolean sw = true;
        String version = redisTemplate.opsForValue().get("v_param_wraptype_version") + "";
        if (wraptypeMap != null) {
            if (versionMap.containsKey("param_wraptype_version")) {
                String str = versionMap.get("param_wraptype_version");
                if (str.equals(version)) {
                    sw = false;
                } else {
                    log.info("当前版本不一致 需要再次获取缓存，缓存版本号：" + version + ",当前版本号：" + str);
                }
            }
        }
        if (sw) {
            Set<String> wraptypeKeys = redisTemplate.keys("param_wraptype_*");
            log.info(">>>>>>>>>>>>redis数据进行加载");
            wraptypeMap = new HashMap<String, Wraptype>();
            for (String wraptypeKey : wraptypeKeys) {
                String wraptypeStr = redisTemplate.opsForValue().get(wraptypeKey);
                // Wraptype wraptype = JSON.parseObject(wraptypeStr, Wraptype.class);
                Wraptype wraptype = gson.fromJson(wraptypeStr, Wraptype.class);
                wraptypeMap.put(wraptypeKey, wraptype);
            }
            log.info("重新获取redis，当前版本号：" + version);
            versionMap.put("param_wraptype_version", version);
        }
        //log.info("包装种类，值："+new Gson().toJson(wraptypeMap));
        return wraptypeMap;
    }

}
